<?php
jimport('joomla.application.component.model');
class svModelPhongban extends JModel
{
	//------------ tim kiem----------------------------------
	function getAllPhongban($param)
	{
		$query='SELECT pb.*,dvql.dvql_ten
			    FROM qlcb_donviquanly as dvql,qlcb_phongban as pb
			    WHERE dvql.dvql_id=pb_iddonviquanly 
			    ORDER BY '.$param['order'].'  '.$param['order_Dir'].'
				LIMIT '.$param['limitstart'].' , '.$param['limit'];
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getPhongbanByID($id)
	{
		$query='SELECT pb.*,dvql.dvql_ten
		FROM qlcb_donviquanly as dvql,qlcb_phongban as pb
		WHERE dvql.dvql_id=pb_iddonviquanly AND pb_id='.$id;
		$result=$this->_loadObject($query);
		return $result;
	}
	function getTotal()
	{
		$query='SELECT COUNT(pb_id) FROM qlcb_phongban ';
		$result=$this->_loadResult($query);
		return $result;
	}
	function Listdonvi()
	{
		$query='SELECT DISTINCT dvql_id as `key`,dvql_ten as `text`  FROM qlcb_donviquanly';
		return $this->_getList($query);
	}
	//----------------------------them ,sua ,xoa -------------------------------
	// xu ly su lieu
	function remove($id)
	{
		$query='DELETE FROM qlcb_phongban WHERE pb_id='.$id;
		$this->_Execute($query);
	}
	// cac ham phuc vu truy van CSDL
	function _loadResult($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadResult();
		return $result;
	}
	function _loadObject($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadObject();
		return $result;
	}
	function _loadObjectList($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadObjectList();
		return $result;
	}
	function _Execute($query,$message)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		try {
			$db->query();
			return $message." thành công ";
		}
		catch (Exception $e){
			return $message." Không thành công! Vui lòng thử lại.";
		}
	}
}
?>